<?php
/**
 * 功能描述：	菜单管理
 * @datetime	2022-07-09
 * @version   	v2.3.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_MenuController extends Controller {

    // 是否登录
    function __construct($controller, $action) {
        parent::__construct($controller, $action);
        $loginSer = new Admin_LoginServe();
        $loginSer->judgeSessionLogin($controller, $action);
    }

    /**
     * Notes: 左侧菜单
     * User: ZhuHaili
     * Date: 2022/7/9
     */
    function index(){
        $navtype = '2';
        $classSer = new Admin_MenuServe();
        $powerSer = new Admin_PowerServe();
        $list = $classSer->getClassList($navtype);
        $powerInfo = $powerSer->getUserPowerInfo();
        $this->set('powerInfo', $powerInfo);
        $this->set('classlist', $list);
        $this->set('navtype', $navtype);
        $this->set('title', '左侧菜单');
        $this->display();
    }

    /**
     * Notes: 顶部菜单
     * User: ZhuHaili
     * Date: 2022/7/9
     */
    function sysnav(){
        $navtype = '1';
        $classSer = new Admin_MenuServe();
        $list = $classSer->getClassList($navtype);
        $this->set('classlist', $list);
        $this->set('navtype', $navtype);
        $this->set('title', '顶部菜单');
        $this->display('index');
    }

    /**
     * Notes: 添加菜单
     * User: ZhuHaili
     * Date: 2022/7/9
     */
    function add(){
        $subFlag = Fun::input('subFlag');
        // 提交数据
        if($subFlag == 'Y') {
            $param = Fun::input();
            $classSer = new Admin_MenuServe();
            $result = $classSer->addClass($param);
            Fun::json($result, true);
        }
        // 映射页面
        $fid = Fun::input('fid');
        $navType = Fun::input('navtype');
        $modelSer = new Admin_MouldServe;
        $sysmould = $modelSer->getMouldsList(1);
        $sysmould = array_merge($sysmould, BaseData::$mouldSeat);
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList($navType);
        // 获得父级的栏目信息（不用再次获取数据库，直接在已经获取的菜单列表中循环找出减轻数据库压力）
        $fClass = array();
        foreach($classlist as $v){
            if($v['id'] == $fid){
                $fClass = $v;
                break;
            }
        }
        $this->set('fid', $fid);
        $this->set('fClass', $fClass);
        $this->set('navType', $navType);
        $this->set('sysmould', $sysmould);
        $this->set('classlist', $classlist);
        $this->set('title', '菜单添加');
        $tmpl = $navType == 1 ? 'addtop' : 'add';
        $this->display($tmpl);
	}

    /**
     * Notes: 修改菜单
     * User: ZhuHaili
     * Date: 2022/7/9
     */
    function update(){
        $subFlag = Fun::input('subFlag');
        $classSer = new Admin_MenuServe();
        // 提交数据
        if($subFlag == 'Y') {
            $param = Fun::input();
            $result = $classSer->updateClass($param);
            Fun::json($result, true);
        }
        // 映射页面
        $id = Fun::input('id');
        $navType = Fun::input('navtype');
        $commModel = new Admin_CommonModel();
        $info = $commModel->getEssayInfo('sysmenu', array('id'=>$id));
        $modelSer = new Admin_MouldServe;
        $sysmould = $modelSer->getMouldsList(1);
        $sysmould = array_merge($sysmould, BaseData::$mouldSeat);
        $fieldSer = new Admin_FieldServe();
        $fields = $fieldSer->getOpenClassFieldsInfos($id,'1','1');
        $fieldItems = FieldItemAdd::setItems($fields, $info);
        $fieldItemHtml = isset($fieldItems['html']) ? $fieldItems['html'] : '';
        $fieldItemStyle = isset($fieldItems['style']) ? $fieldItems['style'] : '';
        $classlist = $classSer->getClassList($navType);
        $this->set('menu_id', $id);
        $this->set('navType', $navType);
        $this->set('info', $info);
        $this->set('sysmould', $sysmould);
        $this->set('classlist', $classlist);
        $this->set('fieldItemHtml', $fieldItemHtml);
        $this->set('fieldItemStyle', $fieldItemStyle);
        $this->set('title', '菜单修改');
        $tmpl = $navType == 1 ? 'updatetop' : 'update';
        $this->display($tmpl);
    }

    /**
     * Notes: 修改菜单-编辑外链模式
     * User: ZhuHaili
     * Date: 2022/7/9
     */
    function updategourl(){
        $classSer = new Admin_MenuServe();
        $id = Fun::input('id');
        $commModel = new Admin_CommonModel();
        $info = $commModel->getEssayInfo('sysmenu', array('id'=>$id));
        $modelSer = new Admin_MouldServe;
        $sysmould = $modelSer->getMouldsList(1);
        $sysmould = array_merge($sysmould, BaseData::$mouldSeat);
        $fieldSer = new Admin_FieldServe();
        $fields = $fieldSer->getOpenClassFieldsInfos($id,'1','1');
        $fieldItems = FieldItemAdd::setItems($fields, $info);
        $fieldItemHtml = isset($fieldItems['html']) ? $fieldItems['html'] : '';
        $fieldItemStyle = isset($fieldItems['style']) ? $fieldItems['style'] : '';
        $classlist = isset($info['navtype']) ? $classSer->getClassList($info['navtype']) : array();
        $this->set('menu_id', $id);
        $this->set('info', $info);
        $this->set('sysmould', $sysmould);
        $this->set('classlist', $classlist);
        $this->set('fieldItemHtml', $fieldItemHtml);
        $this->set('fieldItemStyle', $fieldItemStyle);
        $this->set('title', '菜单编辑');
        $this->display();
    }

    /**
     * Notes: 删除菜单
     * User: ZHL
     * Date: 2022/7/9
     */
    function del(){
        $id = Fun::input('id');
        $menuSer = new Admin_MenuServe();
        $result = $menuSer->del('sysmenu', $id);
        Fun::json($result, true);
    }

    /**
     * Notes: 排序
     * User: ZHL
     * Date: 2022/7/9
     */
    function seq(){
        $param = Fun::input('param');
        $menuSer = new Admin_MenuServe();
        $result = $menuSer->seq('sysmenu', $param);
        Fun::json($result, true);
    }

    /**
     * Notes:顶部子级菜单-内容入口
     * User: ZHL
     * Date: 2022/7/9
     */
    function find(){
        $menu_id = Fun::input('c');
        $classSer = new Admin_MenuServe();
        $list = $classSer->getClassList(1, 0, $menu_id);
        $this->set('fid', $menu_id);
        $this->set('classlist', $list);
        $this->set('title', '子级内容管理');
        $this->display();
    }
}